﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using System.Data;

using CE.User;
using CE.Global;
using CE.Provider;

public partial class Provider_CarInsurance_AddNewCarMaritalStatus : System.Web.UI.Page
{
    string PageName = "CXCI";
    string SubPageName = "CXCIMRSTS";
    public static Int64 _providercarmaritalstatusid = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();
        CheckSubPageAccess();

        if (!IsPostBack)
        {
            DataTable dtMaritalStatus = General.GetComboItem("MaritalStatus", true);
            ddlMaritalStatus.DataSource = dtMaritalStatus;
            ddlMaritalStatus.DataTextField = dtMaritalStatus.Columns[0].ColumnName.ToString();
            ddlMaritalStatus.DataValueField = dtMaritalStatus.Columns[1].ColumnName.ToString();
            ddlMaritalStatus.DataBind();
            ddlMaritalStatus.Items.Insert(0, new ListItem("Select", "-1"));

            usrEffectivityDate.StartYear = CE.Global.General.GetUTCDateTime().Year;
            usrEffectivityDate.EndYear = CE.Global.General.GetUTCDateTime().Year + 2;

            ClearProcess();
            ChangeControlSetting();

            if (Request.Params["CMSID"] != null)
            {
                Int32 _carmaritalstatusid;
                if (Int32.TryParse(Request.Params["CMSID"].ToString(), out _carmaritalstatusid))
                {

                    ShowCarMaritalStatus(_carmaritalstatusid);
                }
            }
        }

    }

    //private void GetCXMaritalStatus()
    //{
    //    CarMaritalStatusDB _carmaritalstatusdb = new CarMaritalStatusDB();
    //    DataTable dtMaritalStatus = _carmaritalstatusdb.GetCXMaritalStatus();
    //    ddlMaritalStatus.DataSource = dtMaritalStatus;
    //    ddlMaritalStatus.DataTextField = "Description";
    //    ddlMaritalStatus.DataValueField = "MaritalStatusId";
    //    ddlMaritalStatus.DataBind();
    //    ddlMaritalStatus.Items.Insert(0, new ListItem("Select", "-1"));
    //}

    private void ShowCarMaritalStatus(Int32 ProviderCarMaritalStatusId)
    {
        CarMaritalStatus _carmaritalstatus = new CarMaritalStatus();
        CarMaritalStatusDB _carmaritalstatusdb = new CarMaritalStatusDB();

        _carmaritalstatus = _carmaritalstatusdb.GetProviderCarMaritalStatus(ProviderCarMaritalStatusId);

        if (_carmaritalstatus != null)
        {
            _providercarmaritalstatusid = Convert.ToInt32(_carmaritalstatus.ProviderCarMaritalStatusId.ToString());
            ddlMaritalStatus.SelectedValue = _carmaritalstatus.CXMaritalStatusId.ToString();
            lblProviderCarMaritalStatusId.Text = _carmaritalstatus.ProviderCarMaritalStatusId.ToString();
            txtMaritalStatus.Text = _carmaritalstatus.MaritalStatusDescription;
            txtSex.Text = _carmaritalstatus.Sex;
            txtRate.Text = _carmaritalstatus.MaritalStatusValue.ToString();
            chkStatus.Checked = Convert.ToBoolean(_carmaritalstatus.Status);

            if (_carmaritalstatus.DInfo1 > CE.Global.General.GetUTCDateTime())
            {
                divNewRate.Visible = true;
                txtNewRate.Text = _carmaritalstatus.FInfo1.ToString();
                rdoEffectivity.SelectedValue = "Yes";
            }
            else
            {
                divNewRate.Visible = false;
                rdoEffectivity.SelectedValue = "No";
            }

            usrEffectivityDate.Day = _carmaritalstatus.DInfo1.Day;
            usrEffectivityDate.Month = _carmaritalstatus.DInfo1.Month;
            usrEffectivityDate.Year = _carmaritalstatus.DInfo1.Year;

        }
    }

    public void EffectiveWhen(object sender, EventArgs e)
    {
        if (rdoEffectivity.SelectedValue == "Yes")
        {
            divNewRate.Visible = true;
        }
        else
        {
            divNewRate.Visible = false;
        }

        usrEffectivityDate.Day = CE.Global.General.GetUTCDateTime().Day;
        usrEffectivityDate.Month = CE.Global.General.GetUTCDateTime().Month;
        usrEffectivityDate.Year = CE.Global.General.GetUTCDateTime().Year;
    }

    public void ClearProcess()
    {
        _providercarmaritalstatusid = 0;
        lblProviderCarMaritalStatusId.Text = "0";
        ddlMaritalStatus.SelectedIndex = 0;
        txtMaritalStatus.Text = "";
        txtSex.Text = "";
        txtRate.Text = "";
        chkStatus.Checked = true;
        lblMessage.Text = "";

    }

    protected void btnClearForm_Click(object sender, EventArgs e)
    {
        ClearProcess();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //CheckValidation();
        //ChangeValidationEffectServerSide("CarMaritalStatus");
        if (Page.IsValid)
        {
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            if (CheckAuthCode(_authcode, 100))
            {
                CarMaritalStatus _carmaritalstatus = new CarMaritalStatus();
                CarMaritalStatusDB _carmaritalstatusdb = new CarMaritalStatusDB();

                int _trantype = 1;
                _carmaritalstatus.ProviderCarMaritalStatusId = _providercarmaritalstatusid;
                if (_carmaritalstatus.ProviderCarMaritalStatusId > 0)
                {
                    _trantype = 2;
                }

                _carmaritalstatus.CXMaritalStatusId = Convert.ToInt32(ddlMaritalStatus.SelectedItem.Value);
                _carmaritalstatus.MaritalStatusDescription = txtMaritalStatus.Text.Trim();
                _carmaritalstatus.Sex = txtSex.Text;
                _carmaritalstatus.MaritalStatusValue = float.Parse(txtRate.Text);
                _carmaritalstatus.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
                _carmaritalstatus.RefCEUserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
                _carmaritalstatus.Status = Convert.ToBoolean(chkStatus.Checked.ToString());

                if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
                {
                    _carmaritalstatus.FInfo1 = float.Parse(txtNewRate.Text);
                    _carmaritalstatus.DInfo1 = usrEffectivityDate.GetDate();

                }
                else
                {
                    _carmaritalstatus.FInfo1 = float.Parse(txtRate.Text);
                    _carmaritalstatus.DInfo1 = CE.Global.General.GetUTCDateTime();

                }
                _carmaritalstatus.ProviderCarMaritalStatusId = _carmaritalstatusdb.Insert(_carmaritalstatus);

                if (_carmaritalstatus.ProviderCarMaritalStatusId > 0)
                {                    
                    InsertProviderAuthTransaction(_carmaritalstatus.ProviderCarMaritalStatusId, "CICarMaritalStatus", _trantype, 100, _authcode, _carmaritalstatus.ProviderCompanyId, _carmaritalstatus.RefCEUserId);
                    ProviderUpdateSave(_carmaritalstatus.ProviderCarMaritalStatusId);
                    ClearProcess();
                    lblMessage.Text = "Saved successfully!";
                }
                else
                {
                    lblMessage.Text = "Saving failed!";
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    private void ProviderUpdateSave(Int64 ProviderRefId)
    {
        ProviderUpdates _providerupdates = new ProviderUpdates();
        ProviderUpdatesDB _providerupdatesDB = new ProviderUpdatesDB();

        _providerupdates.UpdateId = 0;
        _providerupdates.CXTableId = GetCXTableId();
        _providerupdates.ProviderRefId = ProviderRefId;

        if (rdoEffectivity.SelectedValue == "Yes" && usrEffectivityDate.GetDate() > CE.Global.General.GetUTCDateTime())
        {
            //Scheduled
            _providerupdates.UpdateType = 1;
        }
        else
        {
            //Immediate
            _providerupdates.UpdateType = 0;
        }

        _providerupdates.ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]);
        _providerupdates.RefCEUserID = Convert.ToInt64(Session[General.UserId].ToString());

        _providerupdatesDB.Insert(_providerupdates);

    }

    private Int32 GetCXTableId()
    {
        Int32 CXID = 0;

        ProviderTableListings _tablelist = new ProviderTableListings();
        ProviderTableListingsDB _tablelistdb = new ProviderTableListingsDB();

        int ProviderType = 100; //Car Insurance
        string CXTable = "tblProviderCarMaritalStatus";

        _tablelist = _tablelistdb.GetProviderTableListingsSingleItem(ProviderType, Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId]), CXTable);

        if (_tablelist != null)
            CXID = _tablelist.CXTableId;

        return CXID;

    }

    private void ChangeControlSetting()
    {
        ddlMaritalStatus.Attributes.Add("onblur", "ShowValidationEffect('" + ddlMaritalStatus.ClientID + "','" + rfvCXMaritalStatus.ClientID + "');");
        txtMaritalStatus.Attributes.Add("onblur", "ShowValidationEffect('" + txtMaritalStatus.ClientID + "','" + rfvMaritalStatus.ClientID + "');");
        txtSex.Attributes.Add("onblur", "ShowValidationEffect('" + txtSex.ClientID + "','" + rfvSex.ClientID + "');");
        txtRate.Attributes.Add("onblur", "ShowValidationEffect('" + txtRate.ClientID + "','" + rfvRate.ClientID + "');");

    }

    private void CheckValidation()
    {
        rfvCXMaritalStatus.Enabled = true;
        rfvMaritalStatus.Enabled = true;
        rfvSex.Enabled = true;
        rfvRate.Enabled = true;
    }

    private void ChangeValidationEffectServerSide(string strValidationGroup)
    {
        if (rfvMaritalStatus.Enabled && rfvMaritalStatus.ValidationGroup == strValidationGroup)
        {
            rfvMaritalStatus.Validate();
            if (rfvMaritalStatus.IsValid == false)
                txtMaritalStatus.ControlStyle.CssClass = "validationError";
            else
                txtMaritalStatus.ControlStyle.CssClass = "";
        }
        else
            txtMaritalStatus.ControlStyle.CssClass = "";

        if (rfvCXMaritalStatus.Enabled && rfvCXMaritalStatus.ValidationGroup == strValidationGroup)
        {
            rfvCXMaritalStatus.Validate();
            if (rfvCXMaritalStatus.IsValid == false)
                ddlMaritalStatus.ControlStyle.CssClass = "validationError";
            else
                ddlMaritalStatus.ControlStyle.CssClass = "";
        }
        else
            ddlMaritalStatus.ControlStyle.CssClass = "";

        if (rfvSex.Enabled && rfvSex.ValidationGroup == strValidationGroup)
        {
            rfvSex.Validate();
            if (rfvSex.IsValid == false)
                txtSex.ControlStyle.CssClass = "validationError";
            else
                txtSex.ControlStyle.CssClass = "";
        }
        else
            txtSex.ControlStyle.CssClass = "";

        if (rfvRate.Enabled && rfvRate.ValidationGroup == strValidationGroup)
        {
            rfvRate.Validate();
            if (rfvRate.IsValid == false)
                txtRate.ControlStyle.CssClass = "validationError";
            else
                txtRate.ControlStyle.CssClass = "";
        }
        else
            txtRate.ControlStyle.CssClass = "";
    }

    protected void ibtBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("ViewProviderCarMaritalStatus.aspx");
    }

    protected void btnDelete_Click(object sender, ImageClickEventArgs e)
    {
        rfvAuthCode.Validate();
        if (lblProviderCarMaritalStatusId.Text != "0" && txtAuthCode.Text.Trim().Length > 0)
        {
            //Int64 _caryearid = CheckCarYear(Convert.ToInt32(ddlFromYear.Text), Convert.ToInt32(ddlToYear.Text), Convert.ToInt32(ddlOccupation.Text));
            Int64 _maritalstatusid = Convert.ToInt64(lblProviderCarMaritalStatusId.Text);
            Int64 _providercompanyid = Convert.ToInt64(Session[General.ProviderCompanyId].ToString());
            string _authcode = CE.Security.MySecurity.Encrypt(txtAuthCode.Text, true);
            Int64 _refceuserid = Convert.ToInt64(Session[General.UserId].ToString());
            if (CheckAuthCode(_authcode, 100))
            {
                if (_maritalstatusid != 0)
                {
                    CarMaritalStatusDB _carmaritalstatusdb = new CarMaritalStatusDB();
                    if (_carmaritalstatusdb.Delete(_maritalstatusid, _providercompanyid))
                    {
                        InsertProviderAuthTransaction(_maritalstatusid, "CICarMaritalStatus", 3, 100, _authcode, _providercompanyid, _refceuserid);
                        Response.Redirect("ViewProviderCarMaritalStatus.aspx");
                    }
                    else
                    {
                        lblMessage.Text = "Deleting failed.";
                    }
                }
            }
            else
            {
                lblMessage.Text = "Wrong auth code!";
            }
        }
    }

    protected bool CheckAuthCode(string strAuthCode, int intAuthCodeType)
    {
        bool _retValue = false;
        ProviderAuthCode _authcode = new ProviderAuthCode();
        ProviderAuthCodeDB _authcodedb = new ProviderAuthCodeDB();
        _authcode = _authcodedb.GetProviderAuthCode(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()), intAuthCodeType, strAuthCode);
        if (_authcode != null)
        {
            _retValue = true;
        }

        return _retValue;
    }

    protected Int64 InsertProviderAuthTransaction(Int64 TransactionId, string TransactionCode, int TransactionType,
        int AuthCodeType, string AuthCode, Int64 ProviderCompanyId, Int64 RefCEUserId)
    {
        ProviderAuthTransaction _authtrans = new ProviderAuthTransaction();
        ProviderAuthTransactionDB _authtransdb = new ProviderAuthTransactionDB();

        _authtrans.TransactionId = TransactionId;
        _authtrans.TransactionCode = TransactionCode;
        _authtrans.TransactionType = TransactionType;
        _authtrans.AuthCode = AuthCode;
        _authtrans.AuthCodeType = AuthCodeType;
        _authtrans.ProviderCompanyId = ProviderCompanyId;
        _authtrans.RefCEUserID = RefCEUserId;

        _authtrans.AuthTransId = _authtransdb.Insert(_authtrans);
        return _authtrans.AuthTransId;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

    private void CheckSubPageAccess()
    {
        Int64 PageAccess = General.GetProviderSubPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), SubPageName);
        if (PageAccess == 0)
        {
            //Response.Redirect("~/Provider/UserMessage.aspx?errorcode=2000");
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
        else if (PageAccess == 100)
        {
            ibtSubmit.Enabled = false;
            btnNew.Enabled = false;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 200)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = false;
        }
        else if (PageAccess == 300)
        {
            ibtSubmit.Enabled = true;
            btnNew.Enabled = true;
            btnDelete.Enabled = true;
        }
    }

}
